package com.holly.unit.genx;

import cn.hutool.core.util.StrUtil;
import com.holly.unit.gen.api.GenerateCodeApi;
import com.holly.unit.gen.api.config.GenCodeCfg;
import com.holly.unit.gen.api.config.GenFileCfg;
import com.holly.unit.gen.api.pojo.ColumnInfo;
import com.holly.unit.gen.api.pojo.TableInfo;
import com.holly.unit.gen.api.util.RandomUtil;
import com.holly.unit.genx.util.DatabaseExtUtil;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 类描述: 代码生成器实现类
 *
 * @author fxr
 * @version 1.0.0
 * @date 2022/1/14 15:46
 */
public class GenerateCodeOperator extends AbstractGenerateCode implements GenerateCodeApi{

    private GenCodeCfg genCodeCfg;

    public GenerateCodeOperator(GenCodeCfg genCodeCfg) {
        this.genCodeCfg = genCodeCfg;
    }

    @Override
    public Map<String, Object> init(TableInfo tableInfo) throws Exception {
        Map<String,Object> data = new HashMap<>();
        //业务包名
        data.put("bussiPackage",GenCodeCfg.getBussiPackage());
        // 填写包名小写
        data.put("entityPackage",tableInfo.getEntityPackage());
        // 填写实体名首字母大写
        data.put("entityName",tableInfo.getEntityName());
        // 前端vue页面的模块
        data.put("vueModularName",tableInfo.getEntityName().toLowerCase());
        // 填写表名
        data.put("tableName",tableInfo.getTableName());
        //自定义主键
        data.put("primaryKeyField",GenCodeCfg.getDbTableId());

        // 默认字段必须输入个数
        if (tableInfo.getFieldRequiredNum() == null) {
            tableInfo.setFieldRequiredNum(StrUtil.isNotEmpty(GenCodeCfg.getPageFieldReqNum())?Integer.parseInt(GenCodeCfg.getPageFieldReqNum()):-1);
        }

        // 默认生成几个查询条件
        if (tableInfo.getQueryFieldNum() == null) {
            tableInfo.setQueryFieldNum(StrUtil.isNotEmpty(GenCodeCfg.getPageQueryNum())?Integer.parseInt(GenCodeCfg.getPageQueryNum()):-1);
        }

        // 默认一行显示几个字段
        if (tableInfo.getFieldNumInRow() == null) {
            tableInfo.setFieldNumInRow(Integer.parseInt(GenCodeCfg.getFieldInRowNum()));
        }



        // 主参数设置
        data.put("tableVo",tableInfo);

        try {
            List<ColumnInfo> columnInfos = null;
            if (columnInfos == null || columnInfos.size() == 0) {
                // 根据表名查询表字段信息
                columnInfos = DatabaseExtUtil.findAllTableColumns(tableInfo.getTableName());
                Collections.reverse(columnInfos);
            }
            data.put("columns",columnInfos);
            List<ColumnInfo> fieldInfos = null;
            if (fieldInfos == null || fieldInfos.size() == 0) {
                // 获取表字段对应实体信息
                fieldInfos = DatabaseExtUtil.findOriginalTableColumn(tableInfo.getTableName());
                Collections.reverse(fieldInfos);
            }
            data.put("originalColumns",fieldInfos);

            for (ColumnInfo columnInfo:fieldInfos) {
                if (columnInfo.getFieldName().toLowerCase().equals(GenCodeCfg.getDbTableId().toLowerCase())) {
                    data.put("primaryKeyPolicy",columnInfo.getFieldType());
                }
            }
        }catch (Exception e) {
            throw e;
        }
        long serialVersionUID = RandomUtil.randomLong()+RandomUtil.curMills();
        data.put("serialVersionUID",serialVersionUID);
        return data;
    }

    @Override
    public List<String> autoGen(TableInfo info,String path, String template, String customerPath) throws Exception {

        //自定义代码生成路径
        if (path != null && !"".equals(path)) {
            GenCodeCfg.setProjectPath(path);
        }
        //自定义代码生成器的模板路径
        if (template != null && !"".equals(template)){
            GenCodeCfg.setTemplatePath(template);
        }
        this.autoGen(info,customerPath);
        return genFileRes;
    }

    @Override
    public List<String> autoGen(TableInfo info,String customerPath) throws Exception {

        String projectPath = GenCodeCfg.getProjectPath();
        Map<String,Object> templateData = this.init(info);
        String templatePath = GenCodeCfg.getTemplatePath();
        if (trimHeadAndTailChar(templatePath,"/").equals("holly/codeTemplate")){
            templatePath = "/"+trimHeadAndTailChar(templatePath,"/") + "/one";
            GenCodeCfg.setTemplatePath(templatePath);
        }

        GenFileCfg fileCfg = new GenFileCfg(templatePath);
        fileCfg.setCustomePath(customerPath);
        this.genFileAbsolutePath(fileCfg,projectPath,templateData);
        return genFileRes;
    }
}
